package com.fary.hystrix;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Fary
 * @version 1.0
 * @description: TODO
 * @date 2021/9/4 14:22
 */
@RestController
public class HystrixController {

    @Autowired
    private LoadBalancerClient loadBalancer;

    @GetMapping("/choose")
    @HystrixCommand(fallbackMethod = "defaultCallHello",
            commandProperties = {
                    @HystrixProperty(name = "execution.isolation.strategy", value = "THREAD")
            }
    )
    public Object chooseUrl() {
        ServiceInstance instance = loadBalancer.choose("eureka-producer");
        return instance;
    }

    public String defaultCallHello() {
        return "fail";
    }

}
